﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.ExtensionMethods;
using Toolkit.Web.Infastructure;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public sealed class DataGridColumnFactory<T> where T : class
    {
        private readonly DataGrid<T> _container;

        public DataGridColumnFactory(DataGrid<T> container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            _container = container;
        }

        public DataGridColumnBuilder<T> AddColumn(string key)
        {
            DataGridColumn<T> column = new DataGridColumn<T>(_container, key);
            _container.Columns.Add(column);
            return new DataGridColumnBuilder<T>(column);
        }

        public DataGridColumnBuilder<T> AddCheckBoxColumn(bool enableBatchSelection, int width)
        {
            DataGridColumn<T> column = new DataGridColumn<T>(_container, "__cb__");

            if (enableBatchSelection)
            {
                column.HeaderHtmlAttributes.AppendInValue("class", " ", "vc-dg-cbCell");
                column.HeaderTemplate = new HtmlTemplate();
                column.HeaderTemplate.InlineTemplate = h => "<div class='vc-dg-checkbox' data-row-selector='True' data-bulk-action='True'><span class='vc-icon'>&nbsp;</span></div>";
            }

            column.HtmlAttributes.AppendInValue("class", " ", "vc-dg-cbCell");
            column.Template = new HtmlTemplate<T>();
            column.Template.InlineTemplate = item => "<div class='vc-dg-checkbox' data-row-selector='True'><span class='vc-icon'>&nbsp;</span></div>";

            column.Label = HtmlStringExtensions.Create(string.Empty);

            column.Settings.EnableEditing = false;
            column.Settings.EnableHidingAndShowing = false;
            column.Settings.EnableMenu = false;
            column.Settings.EnableResizing = false;
            column.Settings.EnableSorting = false;
            column.Settings.Index = 0;
            column.Settings.IsVisible = true;
            column.Settings.Width = width;

            _container.Columns.Add(column);
            return new DataGridColumnBuilder<T>(column);
        }

        public DataGridColumnBuilder<T> AddIndexColumn(int width)
        {
            DataGridColumn<T> column = new DataGridColumn<T>(_container, "__i__");

            column.HeaderHtmlAttributes.AppendInValue("class", " ", "vc-dg-indexCell");
            column.HeaderTemplate = new HtmlTemplate();
            column.HeaderTemplate.InlineTemplate = item => "&nbsp;";

            column.HtmlAttributes.AppendInValue("class", " ", "vc-dg-indexCell");
            column.Template = new HtmlTemplate<T>();

            int rowIndex = 0;
            column.Template.ActionTemplate = (item, writer) =>
            {
                int curRowIndex = _container.Settings.RecordStartIndex + rowIndex;
                writer.Write(curRowIndex.ToString());
                rowIndex += 1;
            };

            column.Label = HtmlStringExtensions.Create(string.Empty);

            column.Settings.EnableEditing = false;
            column.Settings.EnableHidingAndShowing = false;
            column.Settings.EnableMenu = false;
            column.Settings.EnableResizing = false;
            column.Settings.EnableSorting = false;
            column.Settings.Index = 0;
            column.Settings.IsVisible = true;
            column.Settings.HorizontalAlignment = "right";
            column.Settings.Width = width;

            _container.Columns.Add(column);
            return new DataGridColumnBuilder<T>(column);
        }

        public DataGridColumnBuilder<T> AddChildRowToggleColumn(int width)
        {
            if (_container.Settings.ApplyVirtualizedScrolling)
            {
                throw new NotSupportedException("ChildDataRows are not supported when used in conjunction with row buffering.");
            }

            if (_container.ChildRowAction == null)
            {
                throw new NotSupportedException("No ChildRowAction has been set.");
            }

            DataGridColumn<T> column = new DataGridColumn<T>(_container, "__crt__");

            column.HeaderHtmlAttributes.AppendInValue("class", " ", "vc-dg-crToggleCell");
            column.HeaderTemplate = new HtmlTemplate();
            column.HeaderTemplate.InlineTemplate = item => "&nbsp;";

            column.HtmlAttributes.AppendInValue("class", " ", "vc-dg-crToggleCell");
            column.Template = new HtmlTemplate<T>();

            int index = 0;
            column.Template.ActionTemplate = (item, writer) =>
            {
                DataGridChildRow<T> childRow = new DataGridChildRow<T>(_container, item, index);
                _container.ChildRowAction(childRow);

                if (childRow.Conditional != null && childRow.Conditional(item))
                {
                    writer.Write("<div class='vc-dg-crToggle' data-row-toggle='True'><span class='vc-icon'>&nbsp;</span></div>");
                }

                index += 1;
            };

            column.Label = HtmlStringExtensions.Create(string.Empty);

            column.Settings.EnableEditing = false;
            column.Settings.EnableHidingAndShowing = false;
            column.Settings.EnableMenu = false;
            column.Settings.EnableResizing = false;
            column.Settings.EnableSorting = false;
            column.Settings.Index = 0;
            column.Settings.IsVisible = true;
            column.Settings.Width = width;

            _container.Columns.Add(column);
            return new DataGridColumnBuilder<T>(column);
        }
    }
}
